package main

import (
	//"encoding/json"
	"log"
	"net"
	. "qqtestprotocol"
)

type bussiness struct {
	userConn map[int64]net.Conn
	connUser map[net.Conn]int64
	db       QQtestDB
	Mon      monitor
}

func (t *bussiness) Init() error {
	t.userConn = make(map[int64]net.Conn)
	t.connUser = make(map[net.Conn]int64)
	return t.db.Init()
}

func (t *bussiness) onDisconn(conn net.Conn) error {
	// 反查用户
	uid, ok := t.connUser[conn]
	if !ok {
		return nil
	}
	// 删除在线状态
	delete(t.connUser, conn)
	delete(t.userConn, uid)

	t.Mon.OnlineUserCount--
	return nil
}

func (t *bussiness) Regist(conn net.Conn, buf []byte) error {
	// 	解析协议
	var cmd Protocol_regist
	err := TransProtocol(buf, &cmd)
	if err != nil {
		log.Println("QQtest transProtocol regist err: ", err)
		return err
	}

	// 参数校验
	if len(cmd.Nick) == 0 || len(cmd.Pwd) == 0 {
		return SendErrMsg(&g_tcpServer, conn, "nick name or pwd is empty")
	}

	// 数据库操作
	// regist & check nick name conflict
	data, err1 := t.db.Regist(cmd)
	if err1 != nil {
		return SendErrMsg(&g_tcpServer, conn, err1.Error())
	}

	// 保存登录状态
	t.userConn[data.Uid] = conn
	t.connUser[conn] = data.Uid

	t.Mon.DAU++
	t.Mon.OnlineUserCount++
	// 返回成功消息
	return SendSuccessMsg(&g_tcpServer, conn, data)
}

func (t *bussiness) Login(conn net.Conn, buf []byte) error {
	// 	解析协议
	var cmd Protocol_login
	err := TransProtocol(buf, &cmd)
	if err != nil {
		log.Println("QQtest transProtocol login err: ", err)
		return err
	}

	// 参数校验
	if cmd.Uid <= 0 || len(cmd.Pwd) == 0 {
		return SendErrMsg(&g_tcpServer, conn, "uid or pwd is empty")
	}

	// 数据库操作
	data, err1 := t.db.Login(cmd)
	if err1 != nil {
		return SendErrMsg(&g_tcpServer, conn, err1.Error())
	}

	// 保存登录状态
	t.userConn[data.Uid] = conn
	t.connUser[conn] = data.Uid

	t.Mon.DAU++
	t.Mon.OnlineUserCount++
	// 返回成功消息
	return SendSuccessMsg(&g_tcpServer, conn, data)
}

func (t *bussiness) AddFriend(conn net.Conn, buf []byte) error {
	// 	解析协议
	var cmd Protocol_add_friend
	err := TransProtocol(buf, &cmd)
	if err != nil {
		log.Println("QQtest transProtocol add friend err: ", err)
		return err
	}

	// 参数校验
	if cmd.Uid <= 0 || cmd.Fid <= 0 || conn != t.userConn[cmd.Uid] {
		return SendErrMsg(&g_tcpServer, conn, "parameters are wrong")
	}

	// 数据库操作
	data, err1 := t.db.AddFriend(cmd)
	if err1 != nil {
		return SendErrMsg(&g_tcpServer, conn, err1.Error())
	}

	// 返回成功消息
	return SendSuccessMsg(&g_tcpServer, conn, data)
}

func (t *bussiness) DelFriend(conn net.Conn, buf []byte) error {
	// 	解析协议
	var cmd Protocol_del_friend
	err := TransProtocol(buf, &cmd)
	if err != nil {
		log.Println("QQtest transProtocol del friend err: ", err)
		return err
	}

	// 参数校验
	if cmd.Uid <= 0 || cmd.Fid <= 0 || conn != t.userConn[cmd.Uid] {
		return SendErrMsg(&g_tcpServer, conn, "parameters are wrong")
	}

	// 数据库操作
	data, err1 := t.db.DelFriend(cmd)
	if err1 != nil {
		return SendErrMsg(&g_tcpServer, conn, err1.Error())
	}

	// 返回成功消息
	return SendSuccessMsg(&g_tcpServer, conn, data)
}

func (t *bussiness) SendMsg(conn net.Conn, buf []byte) error {
	// 	解析协议
	var cmd Protocol_msg
	err := TransProtocol(buf, &cmd)
	if err != nil {
		log.Println("QQtest transProtocol sendmsg err: ", err)
		return err
	}

	// 参数校验
	if cmd.Uid <= 0 || cmd.Fid <= 0 || conn != t.userConn[cmd.Uid] {
		return SendErrMsg(&g_tcpServer, conn, "parameters are wrong")
	}

	// 好友在不在线
	conn1, ok := t.userConn[cmd.Fid]
	if !ok {
		return SendErrMsg(&g_tcpServer, conn, "friend is not online")
	}

	t.Mon.TotalMsgToday++
	SendMsg(&g_tcpServer, conn, cmd)
	return SendMsg(&g_tcpServer, conn1, cmd)
}
